/*
 * @Author: xuhongling
 * @Date:   2020-12-27 13:32:17
 * @Last Modified by:   xuhongling
 * @Last Modified time: 2021-01-19 10:04:25
 */

import dayjs from 'dayjs';

let dateTimeFormat = '';
let indexArr: string[] = [];
let oldIndex = '';

// ECharts 时间数据格式化
export default function useDateTimeFormat(value, index) {
  // 判断X轴展示的时间数据
  if (indexArr.length === 0 && index !== undefined) {
    indexArr.push(index);
    oldIndex = index;
  } else if (index !== undefined) {
    if (oldIndex > index) {
      indexArr = [];
    }
    indexArr.push(index);
    oldIndex = index;
  }

  // 时间格式化
  let texts = dayjs(value).format('MM/DD');
  if (dateTimeFormat !== '') {
    const isDiff = dayjs(dateTimeFormat).format('YYYY/MM/DD') === dayjs(value).format('YYYY/MM/DD');
    if (isDiff) {
      texts = dayjs(value).format('HH:mm');
    }
    dateTimeFormat = value;
  } else {
    dateTimeFormat = value;
  }
  // 判断第一个，展示全部日期
  if (indexArr.length < 10 && index === indexArr[0]) {
    texts = `${dayjs(value).format('MM/DD')}\n${dayjs(value).format('YYYY')}`;
  }
  return texts;
}
